<?php
	require '../autoload.php';
	$tid = trim($_POST['tid']);
	$tName = trim($_POST['tName']);
	if(!$tid || !$tName){
		exit('parm is miss or parm is error!');
	}
	if($tid == 'all'){
		$tName = 'all';
	}

	$path = "../Runtime/CacheLefts/{$tName}";
	if(is_file($path)){ 	//如果有临时缓存
		$content = file_get_contents($path);
	//	$arr = unserialize($content);
		$arr = json_decode($content,1);
		$rand = array_rand($arr);
		echo $arr[$rand]['lefts'];
		unset($arr[$rand]);
		if(count($arr) < 1){
			unlink($path);
			exit;
		}
		$content = json_encode($arr);
		file_put_contents($path,$content);
	}else{	//如果没有临时缓存，就读取长期缓存
		$path2 = "../Runtime/Cache_long/{$tName}";
		if(is_file($path2)){	//如果有长期缓存
			if(!is_dir(dirname($path))){
				mkdirs(dirname($path));
			}
			if(copy($path2,$path)){
				echo '浏览完毕！';
			}else{
				echo '拷贝失败！';
			}
		}else{	//如果没有长期缓存
			$mp = new MyPdo();
			if($tid == 'all'){
				$sql = 'select lefts from translate_info where typeid not in (select tid from translate_not_join)';
				$arr = array();
			}else{
				$sql = 'select lefts from translate_info where typeid = ?';
				$arr = array($tid);
			}
			$res = $mp->dql($sql,$arr);
			if(!$res){
				exit;
			}
			shuffle($res);
			$content = json_encode($res);
			$dir = '../Runtime/Cache_long';
			if(!is_dir($dir)){
				mkdirs($dir);
			}
			$path = $dir.'/'.$tName;
			file_put_contents($path, $content);
			if(copy($path2,$path)){
				echo '浏览完毕！';
			}
		}
	}






